package com.jwds.digital.bios.business.service.rule.impl;

import com.jwds.digital.bios.business.core.constants.RuleCodeConstant;
import com.jwds.digital.bios.business.core.infrastructure.dto.dispatch.DispatchParameterReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.dto.dispatch.FiveBasicInformationReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.dto.feign.BasicStockBinReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.vo.feign.BasicStockBinRespVO;
import com.jwds.digital.bios.business.core.utils.FeignResultUtil;
import com.jwds.digital.bios.business.rpc.feign.BasicStockBinFeign;
import com.jwds.digital.bios.business.service.rule.DispatchRuleSortInterface;
import com.jwds.digital.bios.business.service.service.dispatch.IDispatchConfiguratService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * 起点设备或者终点设备是否空闲
 * */
@Slf4j
@Component
public class IsTheCarIdle implements DispatchRuleSortInterface {

    @Autowired
    private BasicStockBinFeign basicStockBinFeign;
    @Override
    public String getRuleCode() {
        return RuleCodeConstant.IS_THE_CAR_IDLE;
    }

    @Override
    public List<DispatchParameterReqDTO> sortProcess(List<DispatchParameterReqDTO> dataList) {
        List<DispatchParameterReqDTO> resultList = new ArrayList<>();
        List<BasicStockBinRespVO> list= new ArrayList<>();
        List<FiveBasicInformationReqDTO> fiveBasicInformationReqDTOArrayList = new ArrayList<>();
        if (dataList!=null){
            for (DispatchParameterReqDTO dispatchParameterReqDTO : dataList) {
                if (dispatchParameterReqDTO.getFlowList()!=null&&dispatchParameterReqDTO.getSlot()!=null&&dispatchParameterReqDTO.getDeviceCodeList()!=null&&dispatchParameterReqDTO.getSlotIdentification()!=null){
                    if (dispatchParameterReqDTO.getSlotIdentification().equals("1")){
                        for (String s : dispatchParameterReqDTO.getDeviceCodeList()) {
                            BasicStockBinReqDTO binReqDTO = new BasicStockBinReqDTO();
                            binReqDTO.setDeviceIn(s);
                            //        查询仓的所有信息
                            list.addAll(FeignResultUtil.getDataFromBaseResponse(basicStockBinFeign.list(binReqDTO)))  ;
                        }
                        List<BasicStockBinRespVO> dispatchConfiguratReqDTOS = list.stream().sorted(Comparator.comparing(BasicStockBinRespVO::getState)).collect(Collectors.toList());
                        for (BasicStockBinRespVO dispatchConfiguratReqDTO : dispatchConfiguratReqDTOS) {
                            DispatchParameterReqDTO dto = new DispatchParameterReqDTO();
                            dto.setSlot(dispatchConfiguratReqDTO);
                            dto.setDeviceCodeList(dispatchParameterReqDTO.getDeviceCodeList());
                            dto.setDeviceCodeDefault(dispatchParameterReqDTO.getDeviceCodeDefault());
                            dto.setFlowList(dispatchParameterReqDTO.getFlowList());
                            dto.setState(dispatchParameterReqDTO.getState());
                            dto.setSlotIdentification(dispatchParameterReqDTO.getSlotIdentification());
                            resultList.add(dto);
                        }
                    }else {
                        List<FiveBasicInformationReqDTO> flowList =dispatchParameterReqDTO.getFlowList();
                        for (FiveBasicInformationReqDTO fiveBasicInformationReqDTO : flowList) {
                            if(fiveBasicInformationReqDTO.getOtherSlotList()!=null&&fiveBasicInformationReqDTO.getFlow()!=null&&fiveBasicInformationReqDTO.getDeviceCodeList()!=null){
                                for (String s : fiveBasicInformationReqDTO.getDeviceCodeList()) {
                                    BasicStockBinReqDTO binReqDTO = new BasicStockBinReqDTO();
                                    binReqDTO.setDeviceIn(s);
                                    //        查询仓的所有信息
                                    list.addAll(FeignResultUtil.getDataFromBaseResponse(basicStockBinFeign.list(binReqDTO))) ;
                                }
                            }else {
                                log.info("流程传过来数据为空");
                            }
                        }
                        for (FiveBasicInformationReqDTO informationReqDTO : flowList) {
                            List<BasicStockBinRespVO> dispatchConfiguratReqDTOS = list.stream().sorted(Comparator.comparing(BasicStockBinRespVO::getState)).collect(Collectors.toList());
                            FiveBasicInformationReqDTO fiveBasicInformationReqDTO = new FiveBasicInformationReqDTO();
                            fiveBasicInformationReqDTO.setOtherSlotList(dispatchConfiguratReqDTOS);
                            fiveBasicInformationReqDTO.setDeviceCodeList(informationReqDTO.getDeviceCodeList());
                            fiveBasicInformationReqDTO.setDeviceTypeDefault(informationReqDTO.getDeviceTypeDefault());
                            fiveBasicInformationReqDTO.setOtherSlotDefault(informationReqDTO.getOtherSlotDefault());
                            fiveBasicInformationReqDTO.setFlow(informationReqDTO.getFlow());
                            fiveBasicInformationReqDTOArrayList.add(fiveBasicInformationReqDTO);
                        }
                        DispatchParameterReqDTO dispatchConfiguratReqDTO = new DispatchParameterReqDTO();
                        dispatchConfiguratReqDTO.setFlowList(fiveBasicInformationReqDTOArrayList);
                        dispatchConfiguratReqDTO.setSlot(dispatchParameterReqDTO.getSlot());
                        dispatchConfiguratReqDTO.setDeviceCodeList(dispatchParameterReqDTO.getDeviceCodeList());
                        dispatchConfiguratReqDTO.setSlotIdentification(dispatchParameterReqDTO.getSlotIdentification());
                        dispatchConfiguratReqDTO.setDeviceCodeDefault(dispatchParameterReqDTO.getDeviceCodeDefault());
                        dispatchConfiguratReqDTO.setState(dispatchParameterReqDTO.getState());
                        resultList.add(dispatchConfiguratReqDTO);
                    }
                }else {
                    log.info("流程传过来数据为空");
                }
            }
        }else {
            log.info("流程传过来数据为空");
        }
        return resultList;
    }
}
